package org.imyy.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.jsp.jstl.core.Config;

public class CharacterEncodingFilter implements Filter {
	private String characterEncoding;
	private boolean enabled;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		characterEncoding = null;
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		if(enabled || characterEncoding != null){
			req.setCharacterEncoding(characterEncoding);
			res.setCharacterEncoding(characterEncoding);
		}
		chain.doFilter(req, res);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		characterEncoding = config.getInitParameter("characterEncoding");
		enabled = "true".equalsIgnoreCase(config.getInitParameter("enabled").trim()	);
		System.out.println("字符编码：" + characterEncoding);
		System.out.println("开关：" + enabled);
		
	}
	
}
